home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Aminet 25
/
Aminet 25 (1998)(GTI - Schatztruhe)[!][Jun 1998].iso
/
Aminet
/
misc
/
math
/
Matrix.lha
/
Matrix.c
< prev
next >
Wrap
C/C++ Source or Header
|
1998-04-29
|
4KB
|
140 lines
#include <stdlib.h>
#include <stdio.h>
//#include <conio.h>
#define clrscr() printf("\f");
int main(void)
{
int x, y, z, xx, yy, rows;
float matrix[10][11] = {0}, answer[10][2] = {0}, temp1, temp2;
do
{
clrscr();
printf("Enter the number of rows... (May not exceed 9)");
scanf("%d", &rows);
} while ((rows<0) || (rows>9));
printf("Enter %d numbers to make a matrix.\n", rows*(rows+1));
printf("The first group of numbers keyed in will be the first row.\n");
printf("The second group of numbers will be the second row, etc...\n");
printf("The numbers must be separated by hitting <ENTER>.\n");
for (y = 1; y<=rows; y++)
{
for (x = 1; x<=rows+1; x++)
{
scanf("%f", &matrix[y][x]);
}
}
clrscr();
for (y = 1; y<=rows; y++)
{
for (x = 1; x<=rows+1; x++)
{
printf("%10.0f", matrix[y][x]);
}
printf("\n");
}
printf("\nHit <ENTER> to continue...\n");
while (getch()!=13) ;
for (z = 1; z<=rows-1; z++)
{
for (y = rows-1; y>=z; y--)
{
if (matrix[y+1][z])
{
temp2 = matrix[y+1][z];
if (matrix[y][z])
{
temp1 = matrix[y][z];
for (x = 1; x<=rows+1; x++)
{
matrix[y][x] = matrix[y][x] * temp2;
matrix[y+1][x] = matrix[y+1][x] * temp1;
matrix[y+1][x] = matrix[y][x] - matrix[y+1][x];
matrix[y][x] = matrix[y][x] / temp2;
}
}
else
{
if (matrix[1][z]==0) for (x = 1; x<=rows+1; x++)
matrix[1][x] = matrix[y+1][x] - matrix[1][x];
temp1 = matrix[1][z];
for (x = 1; x<=rows+1; x++)
{
matrix[1][x] = matrix[1][x] * temp2;
matrix[y+1][x] = matrix[y+1][x] * temp1;
matrix[y+1][x] = matrix[1][x] - matrix[y+1][x];
matrix[1][x] = matrix[1][x] / temp2;
}
}
for (x = 1; x<=rows && matrix[y+1][x]==0; x++) ;
if (x > rows)
{
if (matrix[y+1][rows+1]) printf("\nNo solution set.\n");
else printf("\nInfinite solution set.\n");
printf("\nHit <Esc> to exit...\n");
while (getch()!=27) ;
exit(0);
}
clrscr();
for (yy = 1; yy<=rows; yy++)
{
for (xx = 1; xx<=rows+1; xx++)
{
printf("%10.0f", matrix[yy][xx]);
}
printf("\n");
}
printf("\nHit <ENTER> to continue...\n");
while (getch()!=13) ;
}
}
}
for (y = rows; y>=1; y--)
{
for (x = rows; x>=y; x--)
{
if (answer[x][1])
{
matrix[y][x] = matrix[y][x] * answer[x][0];
matrix[y][rows+1] = matrix[y][rows+1]-matrix[y][x];
matrix[y][x] = 0;
}
else
{
matrix[y][x] = matrix[y][rows+1]/matrix[y][x];
answer[x][0] = matrix[y][x];
answer[x][1] = -1;
}
clrscr();
for (yy = 1; yy<=rows; yy++)
{
for (xx = 1; xx<=rows+1; xx++)
{
printf("%10.0f", matrix[yy][xx]);
}
printf("\n");
}
printf("\nHit <ENTER> to continue...\n");
while (getch()!=13) ;
}
}
printf("\n");
for (x = 1; x<rows+1; x++)
{
printf("x^%d = %f\n", x, answer[x][0]);
}
printf("\nHit <Esc> to exit...\n");
while (getch()!=27) ;
exit(0);
}